package org.dromara.system.controller.xcx;

import cn.hutool.json.JSONObject;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.ZtUserCoupon;
import org.dromara.system.domain.bo.*;
import org.dromara.system.domain.vo.ZtUserCouponVo;
import org.dromara.system.domain.vo.ZtVipVo;
import org.dromara.system.service.*;
import org.springframework.http.HttpHeaders;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序-用户管理
 *
 * @author Lion Li
 * @date 2024-05-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xcx/user/vip")
public class UserVipController extends BaseController {

    private final IZtUserVipOrderService ztUserVipOrderService;

    private final IZtUserCouponService ztUserCouponService;

    private final IZtCouponService ztCouponService;

    private final IZtVipService ztVipService;

    /**
     * 小程序用户下单
     */
    @Log(title = "小程序用户下单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/userPlacesAnOrder")
    public R<PrepayWithRequestPaymentResponse> add(@Validated(AddGroup.class) @RequestBody ZtUserVipOrderBo bo) {
        return ztUserVipOrderService.userPlacesAnOrder(bo);
    }

    /**
     * 支付通知
     */
    @PostMapping("/wx/notify_url")
    public JSONObject notify_url(@RequestHeader HttpHeaders headers,@RequestBody String body) {
        return ztUserVipOrderService.wxNotify(headers,body);
    }

    /**
     * 查询用户优惠卷列表
     */
    @GetMapping("/userCoupon/list")
    public TableDataInfo<ZtUserCouponVo> list(ZtUserCouponBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getLoginUser().getUserId());
        pageQuery.setOrderByColumn("createTime");
        pageQuery.setIsAsc("descending");
        return ztUserCouponService.queryPageList_join_user(bo, pageQuery);
    }

    /**
     * 用户领取优惠卷
     */
    @GetMapping("/userCoupon/receive")
    public R<List<ZtUserCoupon>> receive() {
        return R.ok(ztUserCouponService.receive());
    }

    /**
     * 查询用户可交易的VIP列表
     */
    @GetMapping("/list")
    public R<List<ZtVipVo>> list() {
        return R.ok(ztVipService.userlist());
    }

}
